import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;

/**
 * @Author: Routz
 * @since: 2020/6/6 11:51
 */
public class TomcatApplication {
    private static final String CONTEXTPATH = "/context";
    private static final String FILTERNAME = "filter";
    private static final String FILTERURL = "/*";
    private static final String SERVLETNAME = "servlet";
    private static final String SERVLETURL = "/servlet";

    private static final int port = 8080;
    private static final String contextPath = "/context";
    /**
     * http://localhost:8080/context/servlet
     *
     * new Tomcat()
     * tomcat.getService()
     * new StandardServer()
     * catalinaHome
     * new StandardService()
     *
     * @param args
     * @throws LifecycleException
     */
    public static void main(String[] args) throws LifecycleException {
//        Servlet servlet = new HttpServlet() {
//            private static final long serialVersionUID = 1580776670752768845L;
//
//            @Override
//            protected void doGet(HttpServletRequest request, HttpServletResponse response)
//                    throws ServletException, IOException {
//
//                System.out.println("MyServlet 在处理get（）请求...");
//                PrintWriter out = response.getWriter();
//                response.setContentType("text/html;charset=utf-8");
//                out.println("<strong>My Servlet!</strong><br>");
//            }
//
//            @Override
//            protected void doPost(HttpServletRequest request, HttpServletResponse response)
//                    throws ServletException, IOException {
//
//                System.out.println("MyServlet 在处理post（）请求...");
//                PrintWriter out = response.getWriter();
//                response.setContentType("text/html;charset=utf-8");
//                out.println("<strong>My Servlet!</strong><br>");
//            }
//
//        };
//
//        Filter filter = new HttpFilter() {
//            private static final long serialVersionUID = 9184928712221599973L;
//
//            @Override
//            public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
//                System.out.println("doFilter");
//                super.doFilter(request, response, chain);
//            }
//
//            @Override
//            public void destroy() {
//                System.out.println("destroy");
//            }
//        };
//        ServletContainerInitializer initializer = new ServletContainerInitializer() {
//            @Override
//            public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
//                System.out.println("initializer");
//            }
//        };

//        Tomcat tomcat = new Tomcat();
//        tomcat.init();
//        Service service = tomcat.getService();
//        Connector connector = new Connector("HTTP/1.1");
//        connector.setPort(8080);
//        service.addConnector(connector);
//        Engine engine = tomcat.getEngine();
//        service.setContainer(engine);
//        StandardHost host = new StandardHost();
//        host.setName("localhost");
//        engine.addChild(host);
//        Context context = new StandardContext();
//        context.setName("index");
//        context.setPath(CONTEXTPATH);
//        context.addLifecycleListener(new Tomcat.FixContextListener());
//        host.addChild(context);
//        FilterDef filterDef = new FilterDef();
//        filterDef.addInitParameter("key", "value");
//        filterDef.setFilterName(FILTERNAME);
//        filterDef.setFilter(filter);
//        context.addFilterDef(filterDef);
//        FilterMap filterMap = new FilterMap();
//        filterMap.setFilterName(FILTERNAME);
//        filterMap.addURLPattern(FILTERURL);
//        context.addFilterMap(filterMap);
//        context.addParameter("name", "value");
//        Wrapper wrapper = new StandardWrapper();
//        wrapper.addInitParameter("servletkey", "servletvalue");
//        wrapper.setServlet(servlet);
//        wrapper.setName(SERVLETNAME);
//        context.addChild(wrapper);
//        context.addServletMappingDecoded(SERVLETURL, SERVLETNAME);
//        Set set = new HashSet();
//        set.add("A");
//        context.addServletContainerInitializer(initializer, set);
//        context.addApplicationListener("MyServletContextListener");
//        context.addApplicationListener("MyServletContextAttributeListener");
//        context.addApplicationListener("MyServletRequestListener");
//        context.addApplicationListener("MyServletRequestAttributeListener");
//        context.addApplicationListener("MyHttpSessionListener");
//        context.addApplicationListener("MyHttpSessionAttributeListener");
//        tomcat.start();
//        tomcat.getServer().await();
        Tomcat tomcat = new Tomcat();
        tomcat.init();
        String baseDir = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        tomcat.setBaseDir(baseDir);
        tomcat.setPort(port);
        //tomcat.setConnector(new Connector());

        tomcat.addWebapp(contextPath, baseDir);
        tomcat.enableNaming();
        //手动创建
        tomcat.getConnector();
        tomcat.start();
        tomcat.getServer().await();
    }
}
